package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.ReleaseWork;
import com.applet.mallappletbackstage.entity.User;
import com.applet.mallappletbackstage.entity.vo.*;
import com.applet.mallappletbackstage.service.UserService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-07-31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /***
     * 注册用户
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/register")
    public R register(HttpServletRequest request, HttpServletResponse response) {
        String openid = request.getParameter("openid");
        String nickName = request.getParameter("nickName");
        String avatarUrl = request.getParameter("avatarUrl");
        int count = userService.register(openid, nickName, avatarUrl);
        return R.success("注册成功");
    }

    /***
     * 查询用户是否存在
     * @param openId
     * @return
     */
    @GetMapping("/selectIsUser/{openId}")
    public R selectUser(@PathVariable("openId") String openId) {
        User user = userService.selectIsUser(openId);
        if (StringUtils.isEmpty(user)) {
            return R.fail("用户不存在");
        }
        return R.success(user);
    }

    /***
     * 新增用户
     * @param
     * @return
     */
    @PostMapping("/addUser")
    public R addUser(@RequestParam String username,
                     @RequestParam String password,
                     @RequestParam String phone,
                     @RequestParam String nickname,
                     @RequestParam String code) {
        int count = userService.addUser(username, password, nickname, phone, code);
        if (count > 0) {
            return R.success();
        }
        return R.fail("验证码错误");
    }

    /***
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestParam String username,
                   @RequestParam String password) {
        boolean flag = userService.login(username, password);
        if (flag) {
            return R.success("登录成功");
        }
        return R.fail("账号或用户名错误");
    }

    /***
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendCode")
    public R sendCode(@RequestParam String phone) {
        int count = userService.sendCode(phone);
        return R.success("验证码发送成功，请注意接收！");
    }

    /***
     * 通过id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/userInfo/{id}")
    public R userInfo(@PathVariable(value = "id") String id) {
        UserInfoVO user = userService.userInfo(id);
        return R.success(user);
    }

    /***
     * 通过用户id查询用户发布工作列表
     * @param id
     * @return
     */
    @GetMapping("/userReleaseWorkListById/{id}")
    public R userReleaseWorkListById(@PathVariable("id") String id) {
        List<ReleaseWork> list = userService.userReleaseWorkListById(id);
        return R.success(list);
    }

    /***
     * 完善个人信息
     * @param openid
     * @param uInfoVO
     * @return
     */
    @PostMapping("/uInfo/{openid}")
    public R uInfo(@PathVariable(value = "openid") String openid,
                   @RequestBody UInfoVO uInfoVO) {
        int count = userService.uInfo(openid, uInfoVO);
        if (count > 0) {
            return R.success("成功完善个人信息");
        }
        return R.fail("完善个人信息失败");
    }

    /***
     * 通过openid查询用户信息 基础信息使用
     * @param openid
     * @return
     */
    @GetMapping("/info/{openid}")
    public R info(@PathVariable(value = "openid") String openid) {
        UInfoVO uInfoVO = userService.info(openid);
        if (StringUtils.isEmpty(uInfoVO.getUname())) {
            return R.success(null);
        }
        return R.success(uInfoVO);
    }

    /***
     * 通过openid查询用户信息
     * @param openid
     * @return
     */
    @GetMapping("/getInfo/{openid}")
    public R getInfo(@PathVariable(value = "openid") String openid) {
        User user = userService.getInfo(openid);
        return R.success(user);
    }

    /***
     * 完善用户资料
     * @param openId
     * @param perfectInfoVO
     * @return
     */
    @PostMapping("/perfectInfo/{openId}")
    public R perfectInfo(@PathVariable(value = "openId") String openId,
                         @RequestBody PerfectInfoVO perfectInfoVO) {
        int count = userService.perfectInfo(openId, perfectInfoVO);
        return R.success("成功完善资料");
    }

    /***
     * 查询所有用户数据
     * @param openId
     * @return
     */
    @GetMapping("/getAllUserInfo/{openId}")
    public R getAllUserInfo(@PathVariable(value = "openId") String openId) {
        User user = userService.getAllUserInfo(openId);
        return R.success(user);
    }

    /***
     * 获取个人信息
     * @param openId
     * @return
     */
    @GetMapping("/getUserInfo/{openId}")
    public R getUserInfo(@PathVariable(value = "openId") String openId) {
        PersonalInfoVO personalInfoVO = userService.getUserInfo(openId);
        return R.success(personalInfoVO);
    }

    /**
     * 获取用户列表
     *
     * @param current
     * @return
     */
    @GetMapping("/getUserList/{current}")
    public R getUserList(@PathVariable(value = "current") Long current) {
        Page<User> userPage = userService.getUserList(current);
        return R.success(userPage);
    }

    /**
     * 获取用户列表
     *
     * @param current
     * @return
     */
    @PostMapping("/getUserList/date/{current}")
    public R getUserListByDate(@PathVariable(value = "current") Long current, @RequestBody AdminSearch adminSearch) {
        Page<User> userPage = userService.getUserListByDate(current, adminSearch);
        return R.success(userPage);
    }

    /**
     * 拉黑用户
     *
     * @return
     */
    @DeleteMapping("user/{user}")
    public R deleteUser(@PathVariable String user) {
        userService.deleteUser(user);
        return R.success();
    }
}

